package com.xiaojimao.gr_user.Controller;

import com.xaiojimao.gr_common.dto.Result;
import com.xaiojimao.gr_common.vo.req.PageReq;
import com.xiaojimao.Minio.MinioService;
import com.xiaojimao.gr_user.Service.UserService;
import com.xiaojimao.gr_user.pojo.User;
import com.xiaojimao.gr_user.vo.req.UserPermission;
import com.xiaojimao.gr_user.vo.req.UserStatus;
import com.xiaojimao.gr_user.vo.req.UserUpdate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private MinioService minioService;

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
      return userService.userLogin(user);
    }

    @PostMapping("/register")
    public Result register(@Valid @RequestBody User user){
        return userService.userRegister(user);
    }

    @PostMapping("/avatar")
    public Result avatar(MultipartFile file){
        return Result.success( minioService.uploadFile(file, "avatar"));
    }

    @PutMapping("/update")
    public Result update(@Valid @RequestBody UserUpdate user){
        return userService.update(user);
    }

    @GetMapping("/userInfo")
    public Result userInfo(){
        return userService.userInfo();
    }

    @GetMapping("/userInfo/{id}")
    public Result userInfo(@PathVariable String id){
        return userService.getById(id);
    }

    @PutMapping("/status")
    public Result status(@RequestBody UserStatus user){
        return userService.status(user);
    }

    @PutMapping("/permission")
    public Result permission(@RequestBody UserPermission user){
        return userService.permission(user);
    }

    @GetMapping("/list")
    public Result list(@Valid @RequestBody(required = false) PageReq<User> req){
        return userService.list(req);
    }


}
